package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.mapper.ItemMapper;
import com.boot.pojo.Item;
import com.boot.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Override
    public List<Item> getAll(Integer currentPage, Integer pageSize) {
        // 设置分页的参数
        Page<Item> page = new Page<>(currentPage,pageSize);
        //List<Item> list = this.list(page);
        //List<Item> list = this.itemMapper.selectList(page, null);
        this.itemMapper.selectPage(page , null);
        return page.getRecords();
    }

    @Override
    public List<Item> getList(Integer currentPage, Integer pageSize, String itemName, String itemDetail) {
        // 设置分页的参数
        Page<Item> page = new Page<>(currentPage,pageSize);
        // 构建查询的条件
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        // select * from tb_item where id = 1
        //wrapper.eq("id",1);
        //select * from tb_item where item_name like = #{itemName}
        //wrapper.like("item_name",itemName);
        // select * from tb_item where item_name like = #{itemName} and item_detail like = #{itemDetail}
        wrapper.like(StringUtils.isNotEmpty(itemName),"item_name",itemName)
                .or()
                .like(StringUtils.isNotEmpty(itemDetail),"item_detail",itemDetail);

        this.itemMapper.selectPage(page , wrapper);
        return page.getRecords();
    }
    public List<Item> getList2(Integer currentPage, Integer pageSize, String itemName, String itemDetail) {
        // 设置分页的参数
        Page<Item> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(itemName) , Item::getItemName , itemName)
                .or()
                .like(StringUtils.isNotEmpty(itemDetail), Item::getItemDetail,itemDetail);

        this.itemMapper.selectPage(page , wrapper);
        return page.getRecords();
    }
}
